home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / C / x16c.c < prev    next >
C/C++ Source or Header  |  1994-06-30  |  7KB  |  266 lines

  1. /* $Id: x16c.c,v 1.4 1994/06/30 17:57:58 mjl Exp $
  2.  * $Log: x16c.c,v $
  3.  * Revision 1.4  1994/06/30  17:57:58  mjl
  4.  * All C example programs: made another pass to eliminate warnings when using
  5.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  6.  * (now included by plplot.h), eliminated redundant casts, put in more
  7.  * uniform comments, and other minor changes.
  8.  *
  9.  * Revision 1.3  1994/04/25  19:02:55  mjl
  10.  * Increased the number of shade regions to make it a bit more interesting
  11.  * when playing with the palette.
  12.  *
  13.  * Revision 1.2  1994/04/08  12:08:21  mjl
  14.  * Cleaned up some, switched to more interesting test function.
  15.  *
  16.  * Revision 1.1  1994/03/30  07:22:55  mjl
  17.  * Added to test specifically color fill capability of plshade, with optional
  18.  * coordinate mapping.
  19. */
  20.  
  21. /*    x16c.c
  22.  
  23.     plshade demo, using color fill.
  24.  
  25.     Maurice LeBrun
  26.     IFS, University of Texas at Austin
  27.     20 Mar 1994
  28. */
  29.  
  30. #include <plplot.h>
  31.  
  32. #define NCONTR    30        /* Number of contours */
  33. #define XPTS    35        /* Data points in x */
  34. #define YPTS    46        /* Datat points in y */
  35.  
  36. #define XSPA    2./(XPTS-1)
  37. #define YSPA    2./(YPTS-1)
  38.  
  39. static PLFLT clevel[NCONTR];
  40.  
  41. /* Utility macros */
  42.  
  43. #ifndef PI
  44. #define PI    3.1415926535897932384
  45. #endif
  46. #ifndef MAX
  47. #define MAX(a,b)    (((a) > (b)) ? (a) : (b))
  48. #endif
  49. #ifndef MIN
  50. #define MIN(a,b)    (((a) < (b)) ? (a) : (b))
  51. #endif
  52.  
  53. /* Transformation function */
  54.  
  55. PLFLT tr[6] =
  56. {XSPA, 0.0, -1.0, 0.0, YSPA, -1.0};
  57.  
  58. static void
  59. mypltr(PLFLT x, PLFLT y, PLFLT *tx, PLFLT *ty, void *pltr_data)
  60. {
  61.     *tx = tr[0] * x + tr[1] * y + tr[2];
  62.     *ty = tr[3] * x + tr[4] * y + tr[5];
  63. }
  64.  
  65. /* Function prototypes */
  66.  
  67. static void
  68. f2mnmx(PLFLT **f, PLINT nx, PLINT ny, PLFLT *fmin, PLFLT *fmax);
  69.  
  70. /*----------------------------------------------------------------------*\
  71.  * main
  72.  *
  73.  * Does several shade plots using different coordinate mappings.
  74. \*----------------------------------------------------------------------*/
  75.  
  76. int
  77. main(int argc, char *argv[])
  78. {
  79.     int i, j;
  80.     PLFLT x, y, argx, argy, distort;
  81.  
  82.     PLFLT **z, **w, zmin, zmax;
  83.     PLFLT xg1[XPTS], yg1[YPTS];
  84.     PLcGrid  cgrid1;
  85.     PLcGrid2 cgrid2;
  86.  
  87.     PLFLT shade_min, shade_max, sh_color;
  88.     PLINT sh_cmap = 1, sh_width;
  89.     PLINT min_color = 1, min_width = 0, max_color = 0, max_width = 0;
  90.  
  91. /* Parse and process command line arguments */
  92.  
  93.     (void) plParseInternalOpts(&argc, argv, PL_PARSE_FULL);
  94.  
  95. /* Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display */
  96.  
  97.     plscmap0n(3);
  98.  
  99. /* Initialize plplot */
  100.  
  101.     plinit();
  102.  
  103. /* Set up function arrays */
  104.  
  105.     plAlloc2dGrid(&z, XPTS, YPTS);
  106.     plAlloc2dGrid(&w, XPTS, YPTS);
  107.  
  108. /* Set up data array */
  109.  
  110.     for (i = 0; i < XPTS; i++) {
  111.     x = (double) (i - (XPTS / 2)) / (double) (XPTS / 2);
  112.     for (j = 0; j < YPTS; j++) {
  113.         y = (double) (j - (YPTS / 2)) / (double) (YPTS / 2) - 1.0;
  114.  
  115.         z[i][j] = - sin(7.*x) * cos(7.*y) + x*x - y*y;
  116.         w[i][j] = - cos(7.*x) * sin(7.*y) + 2 * x * y;
  117.     }
  118.     }
  119.     f2mnmx(z, XPTS, YPTS, &zmin, &zmax);
  120.     for (i = 0; i < NCONTR; i++)
  121.     clevel[i] = zmin + (zmax - zmin) * (i + 0.5) / (float) NCONTR;
  122.  
  123. /* Set up coordinate grids */
  124.  
  125.     cgrid1.xg = xg1;
  126.     cgrid1.yg = yg1;
  127.     cgrid1.nx = XPTS;
  128.     cgrid1.ny = YPTS;
  129.  
  130.     plAlloc2dGrid(&cgrid2.xg, XPTS, YPTS);
  131.     plAlloc2dGrid(&cgrid2.yg, XPTS, YPTS);
  132.     cgrid2.nx = XPTS;
  133.     cgrid2.ny = YPTS;
  134.  
  135.     for (i = 0; i < XPTS; i++) {
  136.     for (j = 0; j < YPTS; j++) {
  137.         mypltr((PLFLT) i, (PLFLT) j, &x, &y, NULL);
  138.  
  139.         argx = x * PI/2;
  140.         argy = y * PI/2;
  141.         distort = 0.4;
  142.  
  143.         cgrid1.xg[i] = x + distort * cos(argx);
  144.         cgrid1.yg[j] = y - distort * cos(argy);
  145.  
  146.         cgrid2.xg[i][j] = x + distort * cos(argx) * cos(argy);
  147.         cgrid2.yg[i][j] = y - distort * cos(argx) * cos(argy);
  148.     }
  149.     }
  150.  
  151. /* Plot using identity transform */
  152.  
  153.     pladv(0);
  154.     plvpor(0.1, 0.9, 0.1, 0.9);
  155.     plwind(-1.0, 1.0, -1.0, 1.0);
  156.  
  157.     for (i = 0; i < NCONTR; i++) {
  158.     shade_min = zmin + (zmax - zmin) * i / (float) NCONTR;
  159.     shade_max = zmin + (zmax - zmin) * (i +1) / (float) NCONTR;
  160.     sh_color = i / (float) (NCONTR-1);
  161.     sh_width = 2;
  162.     plpsty(0);
  163.  
  164.     plshade(z, XPTS, YPTS, NULL, -1., 1., -1., 1., 
  165.         shade_min, shade_max, 
  166.         sh_cmap, sh_color, sh_width,
  167.         min_color, min_width, max_color, max_width,
  168.         plfill, 1, NULL, NULL);
  169.     }
  170.  
  171.     plcol(1);
  172.     plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0);
  173.     plcol(2);
  174. /*
  175.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, NCONTR, mypltr, NULL);
  176.     */
  177.     pllab("distance", "altitude", "Bogon density");
  178.  
  179. /* Plot using 1d coordinate transform */
  180.     
  181.     pladv(0);
  182.     plvpor(0.1, 0.9, 0.1, 0.9);
  183.     plwind(-1.0, 1.0, -1.0, 1.0);
  184.  
  185.     for (i = 0; i < NCONTR; i++) {
  186.     shade_min = zmin + (zmax - zmin) * i / (float) NCONTR;
  187.     shade_max = zmin + (zmax - zmin) * (i +1) / (float) NCONTR;
  188.     sh_color = i / (float) (NCONTR-1);
  189.     sh_width = 2;
  190.     plpsty(0);
  191.  
  192.     plshade(z, XPTS, YPTS, NULL, -1., 1., -1., 1., 
  193.         shade_min, shade_max, 
  194.         sh_cmap, sh_color, sh_width,
  195.         min_color, min_width, max_color, max_width,
  196.         plfill, 1, pltr1, (void *) &cgrid1);
  197.     }
  198.  
  199.     plcol(1);
  200.     plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0);
  201.     plcol(2);
  202. /*
  203.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, NCONTR,
  204.        pltr1, (void *) &cgrid1);
  205.        */
  206.     pllab("distance", "altitude", "Bogon density");
  207.  
  208. /* Plot using 2d coordinate transform */
  209.  
  210.     pladv(0);
  211.     plvpor(0.1, 0.9, 0.1, 0.9);
  212.     plwind(-1.0, 1.0, -1.0, 1.0);
  213.  
  214.     for (i = 0; i < NCONTR; i++) {
  215.     shade_min = zmin + (zmax - zmin) * i / (float) NCONTR;
  216.     shade_max = zmin + (zmax - zmin) * (i +1) / (float) NCONTR;
  217.     sh_color = i / (float) (NCONTR-1);
  218.     sh_width = 2;
  219.     plpsty(0);
  220.  
  221.     plshade(z, XPTS, YPTS, NULL, -1., 1., -1., 1., 
  222.         shade_min, shade_max, 
  223.         sh_cmap, sh_color, sh_width,
  224.         min_color, min_width, max_color, max_width,
  225.         plfill, 0, pltr2, (void *) &cgrid2);
  226.     }
  227.  
  228.     plcol(1);
  229.     plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0);
  230.     plcol(2);
  231.  
  232.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, NCONTR,
  233.        pltr2, (void *) &cgrid2);
  234.  
  235.     pllab("distance", "altitude", "Bogon density, with streamlines");
  236.  
  237. /* Clean up */
  238.  
  239.     plend();
  240.     free((void *) w);
  241.     free((void *) z);
  242.     exit(0);
  243. }
  244.  
  245. /*----------------------------------------------------------------------*\
  246.  * f2mnmx
  247.  *
  248.  * Returns min & max of input 2d array.
  249. \*----------------------------------------------------------------------*/
  250.  
  251. static void
  252. f2mnmx(PLFLT **f, PLINT nx, PLINT ny, PLFLT *fmin, PLFLT *fmax)
  253. {
  254.     int i, j;
  255.  
  256.     *fmax = f[0][0];
  257.     *fmin = *fmax;
  258.  
  259.     for (i = 0; i < nx; i++) {
  260.     for (j = 0; j < ny; j++) {
  261.             *fmax = MAX(*fmax, f[i][j]);
  262.             *fmin = MIN(*fmin, f[i][j]);
  263.     }
  264.     }
  265. }
  266.